/* ----------------------------------------------------------------------------- CPS_taxsim_main.do

The main control file for imputing tax data to the CPS.

Last updated (this file): 7/10/13

---------------------------------------------------------------------------------------------------
*/

// (for now)
cd C:\Users\Stuart\Documents\dropboxstuff\InsecurityLit

// Prep SSA data (OASDI caps)
cap confirm file CPS_taxsim_ssadata.dta
if _rc!=0 {
	do CPS_taxsim_createssadata
}

// First create tax units
cap confirm file CPS_taxsim_tukey.dta
if _rc!=0 {
	do CPS_taxsim_createtaxunits.do
}

// Next update the taxunit key to identify dependent filers
// and to include a measure of agi for the deduction imputation
cap confirm file CPS_taxsim_tukey2.dta
if _rc!=0 {
	do CPS_taxsim_depfile.do
}


cap confirm file CPS_taxsim_tukeywithdeductions.dta
if _rc!=0 {
	* do CPS_taxsim_benchmarktuandimputation.do
	do CPS_taxsimimputedeductions.do
}


// RUN THE TAXSIM 

use ${ddCPS}/CPS_fullextract, clear

cap drop _merge
merge 1:1 yof h_seq ppos using CPS_taxsim_tukey2
assert _m>1
drop if _m==2
drop _merge

cap drop _merge
merge m:1 yof h_seq taxunit2 using CPS_taxsim_tukeywithdeductions
* tab match _merge
* tab filer_status if match==3&_m<3 // these folks are all non-filers!

cap gen year = yoc
if _rc!=0 cap gen year = yof-1

// this part comes from Judy Scott-Clayton's code
rename state stcps
gen state= . 
replace state=20 if stcps==11  
replace state=30 if stcps==12  
replace state=46 if stcps==13  
replace state=22 if stcps==14  
replace state=40 if stcps==15  
replace state=7  if stcps==16  
replace state=33 if stcps==21  
replace state=31 if stcps==22  
replace state=39 if stcps==23  
replace state=36 if stcps==31  
replace state=15 if stcps==32  
replace state=14 if stcps==33  
replace state=23 if stcps==34  
replace state=50 if stcps==35  
replace state=24 if stcps==41  
replace state=16 if stcps==42  
replace state=26 if stcps==43  
replace state=35 if stcps==44  
replace state=42 if stcps==45  
replace state=28 if stcps==46  
replace state=17 if stcps==47  
replace state=8  if stcps==51  
replace state=21 if stcps==52  
replace state=9  if stcps==53  
replace state=47 if stcps==54  
replace state=49 if stcps==55  
replace state=34 if stcps==56  
replace state=41 if stcps==57  
replace state=11 if stcps==58  
replace state=10 if stcps==59  
replace state=18 if stcps==61  
replace state=43 if stcps==62  
replace state=1  if stcps==63  
replace state=25 if stcps==64  
replace state=4  if stcps==71  
replace state=19 if stcps==72  
replace state=37 if stcps==73  
replace state=44 if stcps==74  
replace state=27 if stcps==81  
replace state=13 if stcps==82  
replace state=51 if stcps==83  
replace state=6  if stcps==84  
replace state=32 if stcps==85  
replace state=3  if stcps==86  
replace state=45 if stcps==87  
replace state=29 if stcps==88  
replace state=48 if stcps==91  
replace state=38 if stcps==92  
replace state=5  if stcps==93  
replace state=2  if stcps==94  
replace state=12 if stcps==95  
label var state "[TS2] State (SOI codes)" 

cap drop mstat
qui gen mstat=.
qui replace mstat=1 if filer_status==2
qui replace mstat=2 if filer_status==1
qui replace mstat=3 if filer_status==3
qui replace mstat=8 if filer_status==4

cap drop depx
cap drop temp
qui gen temp = 1 if inlist(filer_status,4,5)
qui egen depx = total(temp), by(yof h_seq taxunit)
qui replace depx = 0 if filer_status>3

cap drop depchild
cap drop temp
qui gen temp = (age<17) if inlist(filer_status,4,5)
qui egen depchild = total(temp), by(yof h_seq taxunit)
qui replace depchild=0 if filer_status>3

cap drop agex
cap drop temp
qui gen temp = age>64 if mstat<.
qui egen agex = total(temp), by(yof h_seq taxunit2)

// Null variables:
qui gen stcg=0
qui gen ltcg=0
qui gen rentpaid=0
qui gen proptax=0
qui gen otheritem=0
qui gen childcare=0

// Truncate the "individual income" vars (we're just going to collapse them anyway)
foreach v of varlist *_i {
	if "`v'"=="se_wages_i" continue // we did se_wages in CPS_taxsim_depfile.do and it is sure to be more accurate there 
	loc v2 = subinstr("`v'","_i","",.)
	rename `v' `v2'
}
qui gen cnt=1

collapse 	(mean) year state itemized_share* agex ///
			(max) pwages swages mstat depx depchild agi* match fica* oasdi* hi_* ///
			(sum) dividends otherprop pensions gssi transfers ui rentpaid proptax otheritem childcare stcg ltcg ///
			(count) cnt ///
			, by(yof h_seq taxunit2) fast

// mstat is missing for those with 0 income 
drop if mstat==.

// dividends cannot be less than 0
qui replace dividends=max(dividends,0)

// create mortgage var (zero for folks with missing!) 
* summ agi if mstat<.&itemized_share2==., d // agi is less than 10,000 for folks without deductions 
forvalues r=1/10 {
	preserve
		cap drop mortgage
		qui gen mortgage=itemized_share`r'*agi_orig
		qui replace mortgage=0 if mortgage==.

		taxsim9, full 

		cap drop _merge
		gen taxsimid=_n
		merge 1:1 taxsimid using taxsim_out
		tab _merge 

		keep yof h_seq taxunit2 fiitax siitax fica*
		foreach v of varlist fiitax siitax fica* {
			qui egen h`v'_r`r' = total(`v'), by(yof h_seq)
			qui replace h`v'_r`r'=0 if h`v'_r`r'==.
		}
		save CPS_taxsim_tr`r', replace
	restore
}




exit

/*

use ${ddCPS}/CPS_fullextract, clear
cap drop _merge
merge 1:1 yof h_seq ppos using CPS_taxsim_tukey2
assert _m>1
drop if _m==2
drop _merge


cap drop _merge
merge m:1 yof h_seq taxunit2 using CPS_taxsim_tr1
* tab filer_status _m // non-merge is all non-filers!
foreach v of varlist *fiitax *siitax *fica {
	qui replace `v'=0 if `v'==.
}

// for now, rename these to be the first so we can use them in the simulation 
foreach v of varlist *fiitax *siitax *fica {
	rename `v' `v'1
}
keep yof h_seq ppos taxunit* *fiitax* *siitax* *fica* ?wages 

save CPS_fe_taxes, replace



*/
